package com.cmcc.cmii.smsim.rest;

import com.cmcc.cmii.smsim.result.JsonResult;
import com.cmcc.cmii.smsim.utils.jwt.JwtUtil;
import org.springframework.http.ResponseCookie;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import org.tio.http.common.Cookie;

import java.io.IOException;

/**
 * @author Thanks
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 登录获取Token
     * */
    @GetMapping("token")
    public JsonResult getToken(Long userId) {
        System.out.println("-------------------接收到用户信息--------------"+userId);
        return JsonResult.success(JwtUtil.createToken(userId));
    }

    /**
     * 模拟用户登录，写入cookie
     * */
    @GetMapping("login")
    public JsonResult login(String userId, ServerWebExchange exchange) {
        ResponseCookie responseCookie = ResponseCookie.from("cmii-im", userId).build();
        exchange.getResponse().addCookie(responseCookie);
        return JsonResult.success();
    }
}
